<template>
    <el-config-provider :locale="zhCn">
    <div>
        <el-form :model="formData" ref="formRef" label-width="80px" :rules="rules">
            <el-row :gutter="24">
                <el-col :span="24">
                    <div class="grid-content ep-bg-purple-light" style="padding: 15px 0;">
                    <el-form-item label="基本信息" label-position="top"
                        :style="{ fontSize: '15px', fontWeight: 'bold', borderBottom: '1px dashed #e4e7ed' }">
                        <div class="progress-bar">
                        <div class="progress"></div>
                        </div>
                    </el-form-item>
                    </div>
                </el-col>
                <el-col :span="12"> 
                    <el-select 

                        v-model="selectedCandidateId" 
                        placeholder="请选择简历" 
                        style="width: 260px"
                        @change="onCandidateChange"
                        clearable >
                        <el-option 
                        v-for="candidate in candidateList" 
                        :key="candidate.candidateid"
                        :label="candidate.candidatename || '未知姓名'"
                        :value="candidate.candidateid" />
                    </el-select>
                </el-col>
                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="工号" label-position="top" prop="employeeIdNumber" required>
                            <el-input 
                                placeholder="请输入工号" 
                                v-model="formData.employeeIdNumber" 
                                style="width: 260px;" 
                                clearable />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="登录密码" label-position="top" prop="loginPassword" required>
                            <el-input 
                                type="password"
                                placeholder="请输入登录密码" 
                                v-model="formData.loginPassword" 
                                style="width: 260px;" 
                                clearable show-password />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="姓名" label-position="top" prop="employeeName" required>
                            <el-input 
                                placeholder="请输入姓名" 
                                v-model="formData.employeeName" 
                                style="width: 260px;" 
                                clearable />
                        </el-form-item>
                    </div>
                </el-col>

                <!-- <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="身份证" label-position="top" prop="employeeNumber" required>
                            <el-input 
                                placeholder="请输入身份证" 
                                v-model="formData.employeeNumber" 
                                style="width: 260px;" 
                                clearable />
                        </el-form-item>
                    </div>
                </el-col> -->

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="性别" label-position="top" prop="employeeAge" required>
                            <el-select 
                                v-model="formData.employeeAge" 
                                placeholder="请选择性别" 
                                style="width: 260px"
                                placement="bottom">
                                <el-option 
                                    v-for="item in GenderData" 
                                    :key="item.value" 
                                    :label="item.label"
                                    :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="出生日期" label-position="top" prop="employeeBirthdate" required>
                            <el-date-picker
                                v-model="formData.employeeBirthdate"
                                type="date"
                                placeholder="请选择出生日期"
                                style="width: 260px"
                                value-format="YYYY-MM-DD"
                                format="YYYY-MM-DD" />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="籍贯" label-position="top" prop="origin">
                            <el-cascader 
                                v-model="selectedRegion" 
                                :options="regionData" 
                                placeholder="请选择籍贯" 
                                style="width: 260px" 
                                clearable 
                                :teleport="true" />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="户籍类型" label-position="top" prop="householdRegistration" required>
                            <el-select 
                                v-model="formData.householdRegistration" 
                                placeholder="请选择户籍类型" 
                                style="width: 260px" 
                                placement="bottom">
                                <el-option 
                                    v-for="item in HouseholdRegistrationData" 
                                    :key="item.value"
                                    :label="item.label" 
                                    :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="政治面貌" label-position="top" prop="politicalAppearance" required>
                            <el-select 
                                v-model="formData.politicalAppearance" 
                                placeholder="请选择政治面貌" 
                                style="width: 260px"
                                placement="bottom">
                                <el-option 
                                    v-for="item in PoliticalStatusData" 
                                    :key="item.value" 
                                    :label="item.label"
                                    :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="手机号" label-position="top" prop="employeePhone" required>
                            <el-input 
                                placeholder="请输入手机号" 
                                v-model="formData.employeePhone" 
                                style="width: 260px" 
                                clearable />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="婚育情况" label-position="top" prop="maritalStatus" required>
                            <el-select 
                                v-model="formData.maritalStatus" 
                                placeholder="请选择婚育情况" 
                                style="width: 260px"
                                placement="bottom">
                                <el-option 
                                    v-for="item in MarriageAndChildbearingData" 
                                    :key="item.value"
                                    :label="item.label" 
                                    :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="民族" label-position="top" prop="ethnicGroup" required>
                            <el-input 
                                placeholder="请输入民族" 
                                v-model="formData.ethnicGroup" 
                                style="width: 260px" 
                                clearable />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="户籍所在地" label-position="top" prop="placeHouseholdRegistration" required>
                            <el-input 
                                placeholder="请输入户籍所在地" 
                                v-model="formData.placeHouseholdRegistration" 
                                style="width: 260px" 
                                clearable />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="参加工作日期" label-position="top" prop="dateParticipation" required>
                            <el-date-picker
                                v-model="formData.dateParticipation"
                                type="date"
                                placeholder="请选择参加工作日期"
                                style="width: 260px"
                                value-format="YYYY-MM-DD"
                                format="YYYY-MM-DD" />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="紧急联系人" label-position="top" prop="emergencyContactsName" required>
                            <el-input 
                                placeholder="请输入紧急联系人" 
                                v-model="formData.emergencyContactsName" 
                                style="width: 260px" 
                                clearable />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="紧急联系人关系" label-position="top" prop="emergencyContactsRelationship" required>
                            <el-input 
                                placeholder="请输入关系" 
                                v-model="formData.emergencyContactsRelationship" 
                                style="width: 260px" 
                                clearable />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="紧急联系人电话" label-position="top" prop="emergencyContactsPhone" required>
                            <el-input 
                                placeholder="请输入联系电话" 
                                v-model="formData.emergencyContactsPhone" 
                                style="width: 260px" 
                                clearable />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="24">
                    <div class="grid-content ep-bg-purple-light" style="padding: 15px 0;">
                        <el-form-item label="账户信息" label-position="top"
                            :style="{ fontSize: '15px', fontWeight: 'bold', borderBottom: '1px dashed #e4e7ed' }">
                            <div class="progress-bar">
                                <div class="progress"></div>
                            </div>
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="参保城市" label-position="top" prop="insuredCities" required>
                            <el-input 
                                placeholder="请输入参保城市" 
                                v-model="formData.insuredCities" 
                                clearable 
                                style="width: 260px" />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="社保账号" label-position="top" prop="socialSecurity" required>
                            <el-input 
                                placeholder="请输入社保账号" 
                                v-model="formData.socialSecurity" 
                                clearable 
                                style="width: 260px" />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="公积金账号" label-position="top" prop="accumulationFund">
                            <el-input 
                                placeholder="请输入公积金账号" 
                                v-model="formData.accumulationFund" 
                                clearable 
                                style="width: 260px" />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="银行卡号" label-position="top" prop="bankCardNumber" required>
                            <el-input 
                                placeholder="请输入银行卡号" 
                                v-model="formData.bankCardNumber" 
                                clearable 
                                style="width: 260px" />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="开户行" label-position="top" prop="openAnAccount" required>
                            <el-input 
                                placeholder="请输入开户行" 
                                v-model="formData.openAnAccount" 
                                clearable 
                                style="width: 260px" />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="24">
                    <div class="grid-content ep-bg-purple-light" style="padding: 15px 0;">
                        <el-form-item label="入职信息" label-position="top"
                            :style="{ fontSize: '15px', fontWeight: 'bold', borderBottom: '1px dashed #e4e7ed' }">
                            <div class="progress-bar">
                                <div class="progress"></div>
                            </div>
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="入职日期" label-position="top" prop="dateOfInduction" required>
                            <el-date-picker
                                v-model="formData.dateOfInduction"
                                type="date"
                                placeholder="请选择入职日期"
                                style="width: 260px"
                                value-format="YYYY-MM-DD"
                                format="YYYY-MM-DD" />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                    <el-form-item label="入职部门" label-position="top" prop="onboardingDepartment" required>
                        <el-select 
                        v-model="formData.onboardingDepartment" 
                        placeholder="请选择入职部门" 
                        style="width: 260px" 
                        clearable
                        @focus="fetchDepartments">
                        <el-option 
                            v-for="dept in departmentList" 
                            :key="dept.deptId" 
                            :label="dept.deptName" 
                            :value="dept.deptName" />
                        </el-select>
                    </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                    <el-form-item label="入职岗位" label-position="top" prop="entryPosition" required>
                        <el-select 
                        v-model="formData.entryPosition" 
                        placeholder="请选择入职岗位" 
                        style="width: 260px" 
                        clearable
                        @focus="fetchPosts">
                        <el-option 
                            v-for="post in postList" 
                            :key="post.postId" 
                            :label="post.postName" 
                            :value="post.postName" />
                        </el-select>
                    </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="聘用形式" label-position="top" prop="employmentForm" required>
                            <el-input 
                                placeholder="请输入聘用形式" 
                                v-model="formData.employmentForm" 
                                style="width: 260px" 
                                clearable />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="正式薪资" label-position="top" prop="officialSalary" required>
                            <el-input 
                                placeholder="请输入正式薪资" 
                                v-model="formData.officialSalary" 
                                style="width: 260px" 
                                clearable />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="员工状态" label-position="top" prop="employeeStatus" required>
                            <el-input 
                                placeholder="请输入员工状态" 
                                v-model="formData.employeeStatus" 
                                style="width: 260px" 
                                clearable />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="是否有试用期" label-position="top" prop="whetherProbationPeriod" required>
                            <el-select 
                                v-model="formData.whetherProbationPeriod" 
                                placeholder="请选择是否有试用期" 
                                style="width: 260px">
                                <el-option label="是" value="是"></el-option>
                                <el-option label="否" value="否"></el-option>
                            </el-select>
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="试用期薪资" label-position="top" prop="probationarySalary" required>
                            <el-input 
                                placeholder="请输入试用期薪资"
                                v-model="formData.probationarySalary" 
                                style="width: 260px" 
                                clearable />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="试用期天数" label-position="top" prop="probationPeriodTime" required>
                            <el-input 
                                placeholder="请输入试用期天数"
                                v-model="formData.probationPeriodTime" 
                                style="width: 260px" 
                                clearable />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="转正日期" label-position="top" prop="conversionDate" required>
                            <el-date-picker
                                v-model="formData.conversionDate"
                                type="date"
                                placeholder="请选择转正日期"
                                style="width: 260px"
                                value-format="YYYY-MM-DD"
                                format="YYYY-MM-DD" />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="24">
                    <div class="grid-content ep-bg-purple-light" style="padding: 15px 0;">
                        <el-form-item label="教育背景" label-position="top"
                            :style="{ fontSize: '15px', fontWeight: 'bold', borderBottom: '1px dashed #e4e7ed' }">
                            <div class="progress-bar">
                                <div class="progress"></div>
                            </div>
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="最高学历" label-position="top" prop="degree" required>
                            <el-select 
                                v-model="formData.degree" 
                                placeholder="请选择最高学历" 
                                style="width: 260px"
                                placement="bottom">
                                <el-option 
                                    v-for="item in EducationData" 
                                    :key="item.value"
                                    :label="item.label" 
                                    :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="毕业院校" label-position="top" prop="graduatedSchool" required>
                            <el-input 
                                placeholder="请输入毕业院校" 
                                v-model="formData.graduatedSchool" 
                                style="width: 260px" 
                                clearable />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="所需专业" label-position="top" prop="major" required>
                            <el-input 
                                placeholder="请输入所需专业" 
                                v-model="formData.major" 
                                style="width: 260px" 
                                clearable />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="毕业时间" label-position="top" prop="graduatedTime" required>
                            <el-date-picker
                                v-model="formData.graduatedTime"
                                type="date"
                                placeholder="请选择毕业时间"
                                style="width: 260px"
                                value-format="YYYY-MM-DD"
                                format="YYYY-MM-DD" />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="申请时间" label-position="top" prop="applicationDate" required>
                            <el-date-picker
                                v-model="formData.applicationDate"
                                type="date"
                                placeholder="请选择申请时间"
                                style="width: 260px"
                                value-format="YYYY-MM-DD"
                                format="YYYY-MM-DD" />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="24">
                    <div class="grid-content ep-bg-purple-light" style="padding: 15px 0;">
                        <el-form-item label="部门主管审批" label-position="top"
                            :style="{ fontSize: '15px', fontWeight: 'bold', borderBottom: '1px dashed #e4e7ed' }">
                            <div class="progress-bar">
                                <div class="progress"></div>
                            </div>
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="部门经理" label-position="top" prop="departmentHead" required>
                            <el-input 
                                placeholder="请输入部门经理" 
                                v-model="formData.departmentHead" 
                                style="width: 260px" 
                                clearable />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="部门主管意见" label-position="top" prop="departmentHeadOpinion" required>
                            <el-input 
                                placeholder="请输入部门主管意见" 
                                v-model="formData.departmentHeadOpinion" 
                                style="width: 260px" 
                                clearable />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="24">
                    <div class="grid-content ep-bg-purple-light" style="padding: 15px 0;">
                        <el-form-item label="HR确认" label-position="top"
                            :style="{ fontSize: '15px', fontWeight: 'bold', borderBottom: '1px dashed #e4e7ed' }">
                            <div class="progress-bar">
                                <div class="progress"></div>
                            </div>
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="负责HR" label-position="top" prop="responsibleFoHr" required>
                            <el-input 
                                placeholder="请输入负责HR" 
                                v-model="formData.responsibleFoHr" 
                                style="width: 260px" 
                                clearable />
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="12">
                    <div class="grid-content ep-bg-purple-light">
                        <el-form-item label="HR确认" label-position="top" prop="hrConfirm" required>
                            <el-input 
                                placeholder="请输入HR确认" 
                                v-model="formData.hrConfirm" 
                                style="width: 260px" 
                                clearable />
                        </el-form-item>
                    </div>
                </el-col>
            </el-row>
            <hr>
            <el-form-item style="margin-top: 6px;">
                <el-button type="primary" @click="submitForm">提交</el-button>
                <el-button @click="resetForm">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
    </el-config-provider>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'  
import request from '@/api/request'
import { ElMessage } from 'element-plus' 
import { regionData } from 'element-china-area-data'

const formRef = ref(null); 
const selectedRegion = ref([]);
const zhCn = ref(null);
// 添加简历相关变量
const candidateList = ref([]);
const selectedCandidateId = ref('');
const departmentList = ref([]);
const postList = ref([]);

const formData = reactive({
  employeeIdNumber: '',          
  loginPassword: '',            
  employeeName: '',             
  employeeAge: '',              
  employeeNumber: '',           
  employeePhone: '',            
  employeeBirthdate: '',        
  householdRegistration: '',   
  origin: '',                   
  ethnicGroup: '',            
  placeHouseholdRegistration: '', 
  politicalAppearance: '',      
  maritalStatus: '',           
  dateParticipation: '',        
  emergencyContactsName: '',    
  emergencyContactsRelationship: '', 
  emergencyContactsPhone: '',  
  insuredCities: '',          
  socialSecurity: '',           
  accumulationFund: '',         
  bankCardNumber: '',           
  openAnAccount: '',            
  dateOfInduction: '',          
  onboardingDepartment: '',     
  entryPosition: '',           
  employmentForm: '',           
  officialSalary: '',           
  employeeStatus: '在职',       
  whetherProbationPeriod: '是', 
  probationarySalary: '',       
  probationPeriodTime: '90',    
  conversionDate: '',          
  applicationDate: '',          
  departmentHead: '',          
  departmentHeadOpinion: '同意入职', 
  responsibleFoHr: '',         
  hrConfirm: '已确认',       
  degree: '',                
  graduatedSchool: '',         
  major: '',                    
  graduatedTime: ''            
});
// 获取部门列表的方法
const fetchDepartments = async () => {
  if (departmentList.value.length > 0) return; 
  
  try {
    const response = await request({
      url: '/department/list',
      method: 'get'
    });
    
    if (response && response.code === 200) {
      departmentList.value = response.data;
    }
  } catch (error) {
    console.error('获取部门列表失败:', error);
    ElMessage.error('获取部门列表失败');
  }
};
// 获取岗位列表的方法
const fetchPosts = async () => {
  if (postList.value.length > 0) return; 
  
  try {
    const response = await request({
      url: '/system/permission/post/list',
      method: 'get'
    });
    
    if (response && response.code === 200) {
      postList.value = response.data;
    }
  } catch (error) {
    console.error('获取岗位列表失败:', error);
    ElMessage.error('获取岗位列表失败');
  }
};

const fetchCandidateList = async () => {
  try {
    const response = await request({
      url: '/offermanagementtable/findByOffStatus',
      method: 'get',
      params: {
        current: 1,
        size: 100,
        status: '已入职' 
      }
    });
    console.log('获取到的简历数据:', response);
    
    if (response && response.code === 200) {
      if (Array.isArray(response.data.records)) {
        candidateList.value = response.data.records.map(item => ({
          ...item.candidateresume,
          offerId: item.offerid,
          salary: item.salary,  // 正式薪资
          probationarySalary: item.probationarySalary,  // 试用期薪资
          probationPeriod: item.probationPeriod,  // 试用期
          recruitmentPositions: item.recruitmentPositions  // 岗位信息
        })).filter(item => item.candidateid); 
      } else {
        candidateList.value = [];
      }
      console.log('处理后的简历列表:', candidateList.value);
    } else {
      candidateList.value = [];
    }
  } catch (error) {
    console.error('获取简历列表失败:', error);
    ElMessage.error('获取简历列表失败');
    candidateList.value = [];
  }
};



const onCandidateChange = (candidateId) => {
  console.log('选择的简历ID:', candidateId);
  
  if (!candidateId) {
    resetFormData();
    return;
  }
  
  const selectedCandidate = candidateList.value.find(
    candidate => candidate.candidateid === candidateId
  );
  
 console.log('选中的简历数据:', selectedCandidate);
  
  if (selectedCandidate) {
    formData.employeeName = selectedCandidate.candidatename || '';
    formData.employeeAge = selectedCandidate.gender === 0 ? '男' : (selectedCandidate.gender === 1 ? '女' : '');
    formData.employeePhone = selectedCandidate.cellPhone || selectedCandidate.cell_phone || '';
    formData.employeeBirthdate = selectedCandidate.dateOfBirth || selectedCandidate.date_of_birth || '';
    formData.origin = selectedCandidate.origin || '';
    formData.ethnicGroup = selectedCandidate.ethnicGroup || selectedCandidate.ethnic_group || '';
    formData.politicalAppearance = selectedCandidate.politicalAppearance || selectedCandidate.political_appearance || '';
    formData.maritalStatus = selectedCandidate.marriageAndChildbearing || selectedCandidate.marriage_and_childbearing || '';
    formData.graduatedSchool = selectedCandidate.school || '';
    formData.major = selectedCandidate.specialized || selectedCandidate.major || '';
    formData.graduatedTime = selectedCandidate.graduationTime || selectedCandidate.graduation_time || '';
    
    if (selectedCandidate.degree !== undefined) {
      formData.degree = getDegreeLabel(selectedCandidate.degree);
    }
    
    formData.probationarySalary = selectedCandidate.probationarySalary || '';
    formData.officialSalary = selectedCandidate.salary || '';
    
    if (selectedCandidate.probationPeriod) {
      formData.probationPeriodTime = selectedCandidate.probationPeriodTime || '90';
    }
    
    formData.onboardingDepartment = selectedCandidate.recruitmentPositions || '';
    formData.entryPosition = selectedCandidate.recruitmentPositions || '';
    
    console.log('映射后的表单数据:', formData);
  }
};

// 重置表单数据
const resetFormData = () => {
  Object.keys(formData).forEach(key => {
    if (key !== 'employeeStatus' && 
        key !== 'whetherProbationPeriod' && 
        key !== 'probationPeriodTime' && 
        key !== 'departmentHeadOpinion' && 
        key !== 'hrConfirm') {
      formData[key] = '';
    }
  });
};
// 根据学历代码获取学历标签
const getDegreeLabel = (degreeCode) => {
  const degreeMap = {
    1: '专科',
    2: '本科',
    3: '硕士',
    4: '博士'
  };
  return degreeMap[degreeCode] || '';
};
const checkEmployeeIdNumber = async (rule, value, callback) => {
    if (!value) {
        return callback(new Error('请输入工号'));
    }
    
    try {
        const response = await request({
            url: '/into/checkEmployeeId',
            method: 'get',
            params: { 
                employeeIdNumber: value
            }
        });
        
        if (response && response.code === 200 && response.data.exists) {
            callback(new Error('该工号员工已入职，请勿重复添加'));
        } else {
            callback();
        }
    } catch (error) {
        console.error('检查工号失败:', error);
        callback();
    }
};

const rules = {
    employeeIdNumber: [
        { required: true, message: "请输入工号", trigger: "blur" },
        { validator: checkEmployeeIdNumber, trigger: "blur" }
    ],
    loginPassword: [
        { required: true, message: "请输入登录密码", trigger: "blur" },
        { min: 6, message: "密码长度不能少于6位", trigger: "blur" }
    ],
    employeeName: [
        { required: true, message: "请输入员工姓名", trigger: "blur" }
    ],
    employeeNumber: [
        { required: true, message: "请输入身份证号码", trigger: "blur" }
    ],
    employeeAge: [
        { required: true, message: "请选择性别", trigger: "change" }
    ],
    employeeBirthdate: [
        { required: true, message: "请选择出生日期", trigger: "change" }
    ],
    householdRegistration: [
        { required: true, message: "请选择户籍类型", trigger: "change" }
    ],
    ethnicGroup: [
        { required: true, message: "请输入民族", trigger: "blur" }
    ],
    placeHouseholdRegistration: [
        { required: true, message: "请输入户籍所在地", trigger: "blur" }
    ],
    politicalAppearance: [
        { required: true, message: "请选择政治面貌", trigger: "change" }
    ],
    maritalStatus: [
        { required: true, message: "请选择婚育情况", trigger: "change" }
    ],
    dateParticipation: [
        { required: true, message: "请选择参加工作日期", trigger: "change" }
    ],
    emergencyContactsName: [
        { required: true, message: "请输入紧急联系人", trigger: "blur" }
    ],
    emergencyContactsRelationship: [
        { required: true, message: "请输入与紧急联系人的关系", trigger: "blur" }
    ],
    emergencyContactsPhone: [
        { required: true, message: "请输入紧急联系人电话", trigger: "blur" }
    ],
    insuredCities: [
        { required: true, message: "请输入参保城市", trigger: "blur" }
    ],
    socialSecurity: [
        { required: true, message: "请输入社保账号", trigger: "blur" }
    ],
    bankCardNumber: [
        { required: true, message: "请输入银行卡号", trigger: "blur" }
    ],
    openAnAccount: [
        { required: true, message: "请输入开户行", trigger: "blur" }
    ],
    dateOfInduction: [
        { required: true, message: "请选择入职日期", trigger: "change" }
    ],
    onboardingDepartment: [
        { required: true, message: "请输入入职部门", trigger: "blur" }
    ],
    entryPosition: [
        { required: true, message: "请输入入职岗位", trigger: "blur" }
    ],
    employmentForm: [
        { required: true, message: "请输入聘用形式", trigger: "blur" }
    ],
    officialSalary: [
        { required: true, message: "请输入正式薪资", trigger: "blur" }
    ],
    employeeStatus: [
        { required: true, message: "请输入员工状态", trigger: "blur" }
    ],
    whetherProbationPeriod: [
        { required: true, message: "请选择是否有试用期", trigger: "change" }
    ],
    probationarySalary: [
        { required: true, message: "请输入试用期薪资", trigger: "blur" }
    ],
    probationPeriodTime: [
        { required: true, message: "请输入试用期天数", trigger: "blur" }
    ],
    conversionDate: [
        { required: true, message: "请选择转正日期", trigger: "change" }
    ],
    applicationDate: [
        { required: true, message: "请选择申请时间", trigger: "change" }
    ],
    departmentHead: [
        { required: true, message: "请输入部门经理", trigger: "blur" }
    ],
    departmentHeadOpinion: [
        { required: true, message: "请输入部门主管意见", trigger: "blur" }
    ],
    responsibleFoHr: [
        { required: true, message: "请输入负责HR", trigger: "blur" }
    ],
    hrConfirm: [
        { required: true, message: "请输入HR确认", trigger: "blur" }
    ],
    degree: [
        { required: true, message: "请选择最高学历", trigger: "change" }
    ],
    graduatedSchool: [
        { required: true, message: "请输入毕业院校", trigger: "blur" }
    ],
    major: [
        { required: true, message: "请输入所需专业", trigger: "blur" }
    ],
    graduatedTime: [
        { required: true, message: "请选择毕业时间", trigger: "change" }
    ]
};

const GenderData = ref([
    { value: '男', label: '男' },
    { value: '女', label: '女' }
]);

const HouseholdRegistrationData = ref([
    { value: '农村', label: '农村' },
    { value: '城镇', label: '城镇' }
]);

const PoliticalStatusData = ref([
    { value: '群众', label: '群众' },
    { value: '党员', label: '党员' },
    { value: '团员', label: '团员' }
]);

const MarriageAndChildbearingData = ref([
    { value: '未婚', label: '未婚' },
    { value: '已婚', label: '已婚' }
]);

const EducationData = ref([
    { value: '专科', label: '专科' },
    { value: '本科', label: '本科' },
    { value: '硕士', label: '硕士' },
    { value: '博士', label: '博士' }
]);
const submitForm = () => {
    if (!formRef.value) {
        ElMessage.error('表单初始化失败，请刷新页面重试');
        return;
    }
    
    formRef.value.validate(async (valid) => {
        if (valid) {
            try {
                // 处理表单数据
                const submitData = { ...formData };
                // 处理籍贯数据
                if (selectedRegion.value && selectedRegion.value.length > 0) {
                    submitData.origin = selectedRegion.value.join('/');
                }
                
                const response = await request({
                    url: '/into/add',
                    method: 'post',
                    data: submitData
                });
                
                console.log('后端响应:', response);
                
                if (response && response.code === 200) {
                    ElMessage.success('新增成功');
                    resetForm();
                } else {
                    if (response && response.msg && response.msg.includes("员工已入职")) {
                        ElMessage.error("该工号员工已入职，请勿重复添加");
                    } else {
                        ElMessage.error(response?.msg || '新增失败');
                    }
                }
            } catch (error) {
                console.error('提交失败:', error);
                ElMessage.error('新增失败: ' + (error.message || '未知错误'));
            }
        } else {
            ElMessage.warning('请完善必填信息');
        }
    });
};

const resetForm = () => {
    if (formRef.value) {
        formRef.value.resetFields();
    }
    selectedRegion.value = [];
    
    // 重新设置默认值
    formData.employeeAge = '男';
    formData.householdRegistration = '城镇';
    formData.politicalAppearance = '群众';
    formData.ethnicGroup = '汉族';
    formData.maritalStatus = '未婚';
    formData.degree = '本科';
    formData.whetherProbationPeriod = '是';
    formData.probationPeriodTime = '90';
    formData.departmentHeadOpinion = '同意入职';
    formData.hrConfirm = '已确认';
    formData.employeeStatus = '在职';
    
    const today = new Date().toISOString().split('T')[0];
    formData.applicationDate = today;
};

onMounted(() => {
  // 设置下拉框的默认值
  formData.employeeAge = '男';
  formData.householdRegistration = '城镇';
  formData.politicalAppearance = '群众';
  formData.ethnicGroup = '汉族';
  formData.maritalStatus = '未婚';
  formData.degree = '本科';
  formData.whetherProbationPeriod = '是';
  formData.probationPeriodTime = '90';
  formData.departmentHeadOpinion = '同意入职';
  formData.hrConfirm = '已确认';
  formData.employeeStatus = '在职';
  const today = new Date().toISOString().split('T')[0];
  formData.applicationDate = today;
  
  // 获取简历列表
  fetchCandidateList();
  
  fetchDepartments();
  fetchPosts();
});
</script>
<style scoped>
.progress-bar {
    height: 6px;
    background-color: #ccebf7;
    border-radius: 3px;
    display: flex;
    width: 100%;
    overflow: hidden;
}

.progress {
    width: 8%;
    height: 10;
    background-color: #61c3ea;
    border-radius: 3px 0 0 3px;
    transition: width 0.3s ease;
}

.el-radio.is-checked .radio-custom {
    background-color: #61c3ea;
    border-color: #409EFF;
}

.progress {
    width: 8%;
    height: 10;
    background-color: #61c3ea;
    border-radius: 3px 0 0 3px;
    transition: width 0.3s ease;
}

.urban-radio :deep(.el-radio__label) {
    color: #409EFF; /* 蓝色 */
}

.rural-radio :deep(.el-radio__label) {
    color: #67C23A; /* 绿色 */
}

.urban-radio :deep(.el-radio__input.is-checked + .el-radio__label) {
    color: #409EFF; /* 选中时保持蓝色 */
}

.rural-radio :deep(.el-radio__input.is-checked + .el-radio__label) {
    color: #67C23A; /* 选中时保持绿色 */
}

.urban-radio1 :deep(.el-radio__label) {
    color: #409EFF; /* 蓝色 */
}

.rural-radio1 :deep(.el-radio__label) {
    color: #67C23A; /* 绿色 */
}

.rural-radio2 :deep(.el-radio__label) {
    color: #67C23A; /* 绿色 */
}

.urban-radio1 :deep(.el-radio__input.is-checked + .el-radio__label) {
    color: #409EFF; /* 选中时保持蓝色 */
}

.rural-radio1 :deep(.el-radio__input.is-checked + .el-radio__label) {
    color: #67C23A; /* 选中时保持绿色 */
}

.rural-radio2 :deep(.el-radio__input.is-checked + .el-radio__label) {
    color: #67C23A; /* 选中时保持绿色 */
}
</style>